package com.chuangke.admin.controller;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.admin.manager.CacheAdminManager;
import com.chuangke.common.http.HttpResult;

/**
 * 缓存控制器
 * @author chuangke
 * @date Oct 29, 2018
 */
@RestController
@RequestMapping("cache")
public class CacheController {

	@Autowired
	private CacheAdminManager cacheAdminManager;
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/list")
	public HttpResult list() {
		return HttpResult.ok(cacheAdminManager.getAllRegion()) ;
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/find/{key}")
	public HttpResult findByKey(@PathVariable(value = "key") String key) {
		return HttpResult.ok(cacheAdminManager.getCache(key, key)) ;
	}
	
	
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/evict/{key}")
	public HttpResult evict(@PathVariable(value = "key") String key) {
		String[] keys = key.split(",");
		Arrays.asList(keys).forEach(k->{
			cacheAdminManager.evict(k, k);
			cacheAdminManager.clearRegion(k);
			cacheAdminManager.removeRegion(k);
		});
		return HttpResult.ok() ;
	}
	
}
